import { ref } from 'vue';
import { CMotionItem, CMotionProcessItem, CVariableDataItem } from 'stores/editorModels';
import { EnumMotionType, EnumOperatorType } from 'src/util/SelectBoxOption';
import { useCatalogTool } from 'src/hook/common/useCatalogTool';
import { cataItem } from 'src/hook/common/useCatalogType';


export function useMotion () {
  //可见
  let anmVisible = ref<CMotionProcessItem>({
    active: false,
    motionType: EnumMotionType.MOTION_NULL,
    variableId: 0,
    operator: EnumOperatorType.CMP_NULL,
    lowArg: 0,
    highArg: 0
  });

  //缩放
  let anmScale = ref<CMotionProcessItem>({
    active: false,
    motionType: EnumMotionType.MOTION_NULL,
    variableId: 0,
    operator: EnumOperatorType.CMP_NULL,
    lowArg: 0,
    highArg: 0
  });

  //闪烁
  let anmSparkle = ref<CMotionProcessItem>({
    active: false,
    motionType: EnumMotionType.MOTION_NULL,
    variableId: 0,
    operator: EnumOperatorType.CMP_NULL,
    lowArg: 0,
    highArg: 0
  });

  //*********************************************************************************************
  //      读取目录
  //*********************************************************************************************
  const {
    findCatalogLabel
  } = useCatalogTool();

  //读取数据库
  const readVariableTbl = (varTbl: CVariableDataItem[], variableId: number): number | null => {
    let index = varTbl.findIndex((item) => (item.varId === variableId));
    if (index !== -1) {
      return varTbl[index].vData;
    } else {
      return null;
    }
  }

  //配置动效
  const configMotion = (list: CMotionItem[], motionTypeArr: cataItem[], operatorTypeArr: cataItem[]) => {
    if (list.length > 0) {
      list.forEach((item) => {
        let motionType = '';
        if (item.typeId > 0) {
          motionType = findCatalogLabel(motionTypeArr, item.typeId);
          switch (motionType) {
            case EnumMotionType.MOTION_VISIBILITY:
              anmVisible.value.active = false;
              anmVisible.value.motionType = EnumMotionType.MOTION_VISIBILITY;
              anmVisible.value.operator = findCatalogLabel(operatorTypeArr, item.operatorId);
              anmVisible.value.variableId = item.variableId;
              anmVisible.value.lowArg = item.lowArg;
              anmVisible.value.highArg = item.highArg;
              break;
            case EnumMotionType.MOTION_SCALE:
              anmScale.value.active = false;
              anmScale.value.motionType = EnumMotionType.MOTION_SCALE;
              anmScale.value.operator = findCatalogLabel(operatorTypeArr, item.operatorId);
              anmScale.value.variableId = item.variableId;
              anmScale.value.lowArg = item.lowArg;
              anmScale.value.highArg = item.highArg;
              break;
            case EnumMotionType.MOTION_SPARKLE:
              anmSparkle.value.active = false;
              anmSparkle.value.motionType = EnumMotionType.MOTION_SPARKLE;
              anmSparkle.value.operator = findCatalogLabel(operatorTypeArr, item.operatorId);
              anmSparkle.value.variableId = item.variableId;
              anmSparkle.value.lowArg = item.lowArg;
              anmSparkle.value.highArg = item.highArg;
              break;
            default:
              break;
          }
        }
      });
    }
  }

  //执行动效
  const processMotion = (motionType: string, vData: number) => {
    if ((motionType === EnumMotionType.MOTION_VISIBILITY) && (anmVisible.value.variableId > 0)) {
      switch (anmVisible.value.operator) {
        case EnumOperatorType.CMP_EQ:
          if (vData === anmVisible.value.lowArg) {
            anmVisible.value.active = true;
          } else {
            anmVisible.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_LT:
          if (vData < anmVisible.value.highArg) {
            anmVisible.value.active = true;
          } else {
            anmVisible.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_GT:
          if (vData > anmVisible.value.lowArg) {
            anmVisible.value.active = true;
          } else {
            anmVisible.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_LTE:
          if (vData <= anmVisible.value.highArg) {
            anmVisible.value.active = true;
          } else {
            anmVisible.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_GTE:
          if (vData >= anmVisible.value.lowArg) {
            anmVisible.value.active = true;
          } else {
            anmVisible.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_BW_IN:
          if ((vData > anmVisible.value.lowArg) && (vData < anmVisible.value.highArg)) {
            anmVisible.value.active = true;
          } else {
            anmVisible.value.active = false;
          }
          break;
        default:
          break;
      }
    } else if ((motionType === EnumMotionType.MOTION_SCALE) && (anmScale.value.variableId > 0)) {
      switch (anmScale.value.operator) {
        case EnumOperatorType.CMP_EQ:
          if (vData === anmScale.value.lowArg) {
            anmScale.value.active = true;
          } else {
            anmScale.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_LT:
          if (vData < anmScale.value.highArg) {
            anmScale.value.active = true;
          } else {
            anmScale.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_GT:
          if (vData > anmScale.value.lowArg) {
            anmScale.value.active = true;
          } else {
            anmScale.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_LTE:
          if (vData <= anmScale.value.highArg) {
            anmScale.value.active = true;
          } else {
            anmScale.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_GTE:
          if (vData >= anmScale.value.lowArg) {
            anmScale.value.active = true;
          } else {
            anmScale.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_BW_IN:
          if ((vData > anmScale.value.lowArg) && (vData < anmScale.value.highArg)) {
            anmScale.value.active = true;
          } else {
            anmScale.value.active = false;
          }
          break;
        default:
          break;
      }
    } else if ((motionType === EnumMotionType.MOTION_SPARKLE) && (anmSparkle.value.variableId > 0)) {
      switch (anmSparkle.value.operator) {
        case EnumOperatorType.CMP_EQ:
          if (vData === anmSparkle.value.lowArg) {
            anmSparkle.value.active = true;
          } else {
            anmSparkle.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_LT:
          if (vData < anmSparkle.value.highArg) {
            anmSparkle.value.active = true;
          } else {
            anmSparkle.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_GT:
          if (vData > anmSparkle.value.lowArg) {
            anmSparkle.value.active = true;
          } else {
            anmSparkle.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_LTE:
          if (vData <= anmSparkle.value.highArg) {
            anmSparkle.value.active = true;
          } else {
            anmSparkle.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_GTE:
          if (vData >= anmSparkle.value.lowArg) {
            anmSparkle.value.active = true;
          } else {
            anmSparkle.value.active = false;
          }
          break;
        case EnumOperatorType.CMP_BW_IN:
          if ((vData > anmSparkle.value.lowArg) && (vData < anmSparkle.value.highArg)) {
            anmSparkle.value.active = true;
          } else {
            anmSparkle.value.active = false;
          }
          break;
        default:
          break;
      }
    }
  }

  return {
    anmVisible,
    anmScale,
    anmSparkle,
    readVariableTbl,
    configMotion,
    processMotion
  }
}
